package com.pudu.mydemo.act;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.pudu.mydemo.utils.MLog;
import com.pudutech.pdrobot.R;

import java.util.Date;

/**
 * Created by guiliu on 2016/7/14.
 */
public class BaseHomeActivity  extends  AppCompatActivity implements View.OnClickListener{
    protected ActionBar mActionBar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!hasActionBar()) {
          supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                    WindowManager.LayoutParams. FLAG_FULLSCREEN);
            getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }

        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        mActionBar = getSupportActionBar();

        if (hasActionBar()) {
            initActionBar(mActionBar);
        }

        initView();

        /* 初始取得User可触碰屏幕的时间 */
        lastUpdateTime = new Date(System.currentTimeMillis());
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
    }


    private Handler mHandler01 = new Handler();
    private Handler mHandler02 = new Handler();

    /*时间间隔*/
    private long intervalScreenSaver = 1000;
    private long intervalKeypadeSaver = 1000;

    @Override
    protected void onResume() {

        try {
            isRunScreenSaver = false;
            lastUpdateTime = new Date(System.currentTimeMillis());
            mHandler01.postAtTime(mTask01, intervalKeypadeSaver);
        }catch(Exception e)
        {
            e.printStackTrace();
        }

        super.onResume();
        initEvent();
        initData();
        MLog.d("APP", this.getClass().toString()+"@onResume");
    }


    @Override
    protected void onPause() {
        mHandler01.removeCallbacks(mTask01);
        mHandler02.removeCallbacks(mTask02);
        isRunScreenSaver = false;
        lastUpdateTime = new Date(System.currentTimeMillis());

        super.onPause();
        MLog.d("APP", this.getClass().toString()+"@onPause");
    }

    public void initView() {

    }

    public void initData() {

    }

    public void initEvent() {

    }

    protected boolean hasActionBar() {
        return  false;
    }

    protected int getLayoutId() {
        return 0;
    }
    protected int getActionBarTitle() {
        return R.string.app_name;
    }

    protected boolean hasBackButton() {
        return false;
    }

    protected void initActionBar(ActionBar actionBar) {
        if (actionBar == null)
            return;
        if (hasBackButton()) {
            mActionBar.setDisplayHomeAsUpEnabled(true);
            mActionBar.setHomeButtonEnabled(true);
        } else {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
            actionBar.setDisplayUseLogoEnabled(false);
            int titleRes = getActionBarTitle();
            if (titleRes != 0) {
                actionBar.setTitle(titleRes);
            }
        }
    }

    @Override
    public void onClick(View v) {
        MLog.d("APP", this.getClass().toString()+"@onClick:"+getClickAction(v));
    }


    public String getClickAction(View v) {
        return "";
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        updateUserActionTime();
        return super.dispatchKeyEvent(event);
    }

    /* 上一次User有动作的Time Stamp */
    private Date lastUpdateTime;
    /* 计算User有几秒没有动作的 */
    private long timePeriod;

    /*用户有操作的时候不断重置静止时间和上次操作的时间*/
    public void updateUserActionTime() {
        try {
            Date timeNow = new Date(System.currentTimeMillis());
            timePeriod = timeNow.getTime() - lastUpdateTime.getTime();
            lastUpdateTime.setTime(timeNow.getTime());
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    /* 静止超过N秒将自动进入屏保 */
    private float mHoldStillTime = 10;
    /*标识当前是否进入了屏保*/
    private boolean isRunScreenSaver;

    /**
     * 计时线程
     */
    private Runnable mTask01 = new Runnable() {

        @Override
        public void run() {
            try {
                Date timeNow = new Date(System.currentTimeMillis());
            /* 计算User静止不动作的时间间距 */
                /**当前的系统时间 - 上次触摸屏幕的时间 = 静止不动的时间**/
                timePeriod = (long) timeNow.getTime() - (long) lastUpdateTime.getTime();

            /*将静止时间毫秒换算成秒*/
                float timePeriodSecond = ((float) timePeriod / 1000);

                if (timePeriodSecond > mHoldStillTime) {
                    if (isRunScreenSaver == false) {  //说明没有进入屏保
                    /* 启动线程去显示屏保 */
                        mHandler02.postAtTime(mTask02, intervalScreenSaver);

                    } else {
                    /*屏保正在显示中*/
                    }
                } else {
                /*说明静止之间没有超过规定时长*/
                    isRunScreenSaver = false;
                }
            /*反复调用自己进行检查*/
                mHandler01.postDelayed(mTask01, intervalKeypadeSaver);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    };


    /**
     * 持续屏保显示线程
     */
    private Runnable mTask02 = new Runnable() {

        @Override
        public void run() {
            try {
                // TODO Auto-generated method stub
                if (isRunScreenSaver == false) {  //如果屏保正在显示，就计算不断持续显示
                    showCustomView();
                    isRunScreenSaver = true;
                } else {
                    mHandler02.removeCallbacks(mTask02);  //如果屏保没有显示则移除线程
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    };

    private void showCustomView()
    {
        Log.i("eeee", "showCustomView");

        //Intent intent = new Intent(this, CustomViewActivity.class);
        //startActivity(intent);
    }
}
